﻿using PigeonCoopToolkit.Navmesh2D;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;



public class PathFollower : MonoBehaviour
{

    Vector3 screenPosition,mousePositionOnScreen,mousePositionInWorld;
    // public Transform pathingTarget;
    private List<Vector2> path;
	
	// LateUpdate is called once per frame
	void Update () {

       
        screenPosition = Camera.main.WorldToScreenPoint(transform.position);  
        mousePositionOnScreen = Input.mousePosition;   
        mousePositionOnScreen.z = screenPosition.z;  
        mousePositionInWorld =  Camera.main.ScreenToWorldPoint(mousePositionOnScreen);
	    // if(Input.GetKeyDown(KeyCode.E))

        //鼠标左键点击
       if(Input.GetMouseButtonDown(0))
	    {

          //test
        //    Debug.Log(pathingTarget.position.x);
        //    Debug.Log(pathingTarget.position.y);

	        // path = NavMesh2D.GetSmoothedPath(transform.position, pathingTarget.position);
            path = NavMesh2D.GetSmoothedPath(transform.position, mousePositionInWorld);
	    }

        if(path != null && path.Count != 0)
        {
            transform.position = Vector2.MoveTowards(transform.position, path[0], 5*Time.deltaTime);
            if(Vector2.Distance(transform.position,path[0]) < 0.01f)
            {
                path.RemoveAt(0);
            }
        }
	}
}
